package com.swallow.cloud.common.security.shiro;

import com.swallow.cloud.entity.User;
import com.swallow.cloud.service.SystemService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @Author: L.swallow
 * @Date: 2019/5/6 17:45
 */
public class SystemAuthRealm  extends AuthorizingRealm {

    private static Logger logger = LogManager.getLogger(SystemAuthRealm.class);


    @Autowired
    private SystemService systemService;
    /**
     * 登录信息认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        logger.debug("登录中");
        User user =systemService.selectUserByUserName(token.getUsername());
        return new SimpleAuthenticationInfo(token.getPrincipal(), user.getPassword().toCharArray(), getName());
    }

    /**
     * 权限认证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.debug("权限加载中");
        return null;
    }

    protected FormToken getFormToken(AuthenticationToken authcToken) {
        return authcToken instanceof FormToken ? (FormToken)authcToken : null;
    }

}
